package ik.xcj.hostpital.manage.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ik.xcj.hostpital.manage.ex.ServiceException;
import ik.xcj.hostpital.manage.mapper.HospitalSetMapper;
import ik.xcj.hostpital.manage.mapper.IHospitalMapper;
import ik.xcj.hostpital.manage.pojo.entity.Hospital;
import ik.xcj.hostpital.manage.pojo.vo.HospitalVO;
import ik.xcj.hostpital.manage.service.IHospitalService;
import ik.xcj.hostpital.manage.web.JsonResult;
import ik.xcj.hostpital.manage.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class IHospitalServiceImpl implements IHospitalService {

    @Autowired
    IHospitalMapper iHospitalMapper;

    @Autowired
    HospitalSetMapper hospitalSetMapper;

    @Override
    public void delete(Integer id) {
        HospitalVO hospital = iHospitalMapper.getStandardById(id);
        if(hospital == null){
            String message = "删除医院详情失败，该医院不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        String hoscode = hospital.getHoscode();
        int i = hospitalSetMapper.countByHoscode(hoscode);
        if(i > 0){
            hospitalSetMapper.deleteByHoscode(hoscode);
        }
        iHospitalMapper.deleteById(id);
    }

    @Override
    public HospitalVO selectById(Integer id) {
        log.debug("开始处理【查询医院详情的业务】");
        HospitalVO hospital = iHospitalMapper.getStandardById(id);
        if(hospital == null){
            String message = "查询医院不存在";
            log.error(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.debug("完成【查询医院详情的业务】,参数为:{}",hospital);
        return hospital;
    }

    @Override
    public PageInfo<HospitalVO> list(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        log.debug("开始处理【查询医院列表详情的业务】");
        List<HospitalVO> list = iHospitalMapper.list();
        log.debug("完成【查询医院列表详情的业务】,参数为:{}",list);
        return new PageInfo<>(list);
    }

}
